#include "CreditsState.h"

#include "Game.h"
#include "MainMenuState.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_XAudio2.h"


void CreditsState::Enter() 
{	
	timer		= 0.0f;
	timeDisplay = 4.5f;
	curCredit	= 0;

	camX = 0;
	camY = 0;
	moving = false;

	font = CBitmapFont::GetInstance();

	nextCamX = credits[1].camX;
	nextCamY = credits[1].camY;

	background	= CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Menu Images/creditsBackground.png"));
	music		= CSGD_XAudio2::GetInstance()->MusicLoadSong(_T("Sound/Music/music_credits.xwm"));
	CSGD_XAudio2::GetInstance()->MusicStopSong(Game::GetInstance()->GetMenuMusic());
	CSGD_XAudio2::GetInstance()->MusicPlaySong(music);

	alpha = 255;
	alphaTimer = 0.0f;

	InitCredits();
}

void CreditsState::Exit() 
{
	if(background != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(background);
		background = -1;
	}

	if(music != -1)
	{
		CSGD_XAudio2::GetInstance()->MusicStopSong(music);
		CSGD_XAudio2::GetInstance()->MusicUnloadSong(music);
		music = -1;
	}
}

bool CreditsState::Input() 
{
	// If escape return to main menu
	if(CSGD_DirectInput::GetInstance()->JoystickButtonPressed(1) || CSGD_DirectInput::GetInstance()->JoystickButtonPressed(6))
		Game::GetInstance()->ChangeState(MainMenuState::GetInstance());

	return true;
}

void CreditsState::Update(float elapsed) 
{
	if(moving == false)
		timer += elapsed;
	else if(moving == true)
	{
		//int x = abs(nextCamX - camX);
		//int y = abs(nextCamY - camY);
		//int moveX = 400;
		//int moveY = 400;

		//if(x > y)
		//	moveX = 500;
		//else
		//	moveY = 500;

		if(camX != nextCamX)
		{
			if(camX < nextCamX)
				camX += .5f;
			else if(camX > nextCamX)
				camX -= .5f;
		}
		//if(camX > (nextCamX-50) && camX < (nextCamX+50))
		//	camX = nextCamX;

		if(camY != nextCamY)
		{
			if(camY < nextCamY)
				camY += .5f;
			else if(camY > nextCamY)
				camY -= .5f;
		}
		//if(camY > (nextCamY-50) && camY < (nextCamY+50))
		//	camY = nextCamY;
		

		if(camX == nextCamX && camY == nextCamY)
			moving = false;


		//if(camX != nextCamX)
		//{
		//	if(camX < nextCamX)
		//		camX += int(moveX*elapsed);
		//	else if(camX > nextCamX)
		//		camX -= int(moveX*elapsed);
		//}
		//if(camX > (nextCamX-50) && camX < (nextCamX+50))
		//	camX = nextCamX;

		//if(camY != nextCamY)
		//{
		//	if(camY < nextCamY)
		//		camY += int(moveY*elapsed);
		//	else if(camY > nextCamY)
		//		camY -= int(moveY*elapsed);
		//}
		//if(camY > (nextCamY-50) && camY < (nextCamY+50))
		//	camY = nextCamY;
		//

		//if(camX == nextCamX && camY == nextCamY)
		//	moving = false;
	}

	if(timer >= timeDisplay-1.0f)
	{
		alphaTimer += elapsed;
		alpha = (255-int(255*alphaTimer));
	}


	if(timer >= timeDisplay)
	{
		curCredit += 1;

		if(curCredit == 13)
			Game::GetInstance()->ChangeState(MainMenuState::GetInstance());

		moving = true;
		nextCamX = credits[curCredit].camX;
		nextCamY = credits[curCredit].camY;

		timer = 0.0f;
		alphaTimer = 0.0f;
		alpha = 255;
	}
}

void CreditsState::Render() 
{
	//// Game Name
	//Game::GetInstance()->DrawBitmapFont("RoBHERT", 32, 32-moveUpSpeed, 3.0f, D3DCOLOR_ARGB(255,0,255,255));

	//// Group Names
	//Game::GetInstance()->DrawBitmapFont("Team Boomsauce Members:", 140, 150-moveUpSpeed, 1.0f, D3DCOLOR_ARGB(255,0,255,255));
	//Game::GetInstance()->DrawBitmapFont("Colby Yeates", 340, 250-moveUpSpeed, 1.0f, D3DCOLOR_ARGB(255,0,255,255));
	//Game::GetInstance()->DrawBitmapFont("Sean James", 340, 350-moveUpSpeed, 1.0f, D3DCOLOR_ARGB(255,0,255,255));
	//Game::GetInstance()->DrawBitmapFont("Brandon Jones", 340, 450-moveUpSpeed, 1.0f, D3DCOLOR_ARGB(255,0,255,255));
	//Game::GetInstance()->DrawBitmapFont("Benjamin Feldmann", 340, 550-moveUpSpeed, 1.0f, D3DCOLOR_ARGB(255,0,255,255));

	//// Other important people
	//Game::GetInstance()->DrawBitmapFont("Executive Producer:", 140, 650-moveUpSpeed, 1.0f, D3DCOLOR_ARGB(255,0,255,255));
	//Game::GetInstance()->DrawBitmapFont("John O' Leske", 340, 750-moveUpSpeed, 1.0f, D3DCOLOR_ARGB(255,0,255,255));
	//Game::GetInstance()->DrawBitmapFont("Associate Producer:", 140, 850-moveUpSpeed, 1.0f, D3DCOLOR_ARGB(255,0,255,255));
	//Game::GetInstance()->DrawBitmapFont("Robert Martinez", 340, 950-moveUpSpeed, 1.0f, D3DCOLOR_ARGB(255,0,255,255));

	//// Return to main menu after everything is off screen
	//int lastOff = 950-moveUpSpeed;
	//if(lastOff <= 0)
	//	moveUpSpeed = -1*550;
	//	//Game::GetInstance()->ChangeState(MainMenuState::GetInstance());

	CSGD_TextureManager::GetInstance()->Draw(background,int(0 - camX),int(0 - camY));

	DWORD noAlpha =  D3DCOLOR_ARGB(alpha,0,255,255);
	DWORD withAlpha =  D3DCOLOR_ARGB(alpha,alpha,0,0);

	// Render Current Credit
	if(alpha == 255)
		font->Print(credits[curCredit].name.c_str(),int(credits[curCredit].x - camX),int(credits[curCredit].y - camY),1.5f,noAlpha);
	else
		font->Print(credits[curCredit].name.c_str(),int(credits[curCredit].x - camX),int(credits[curCredit].y - camY),1.5f,withAlpha);
}

CreditsState::CreditsState() 
{
	timer		= 0.0f;
	timeDisplay = 5.0f;
	curCredit	= 0;

	camX = 0;
	camY = 0;
	moving = false;

	background = -1;

	alpha = 255;
	alphaTimer = 0.0f;

	music = -1;
}

CreditsState::CreditsState(CreditsState& menu) 
{
}

CreditsState* CreditsState::GetInstance() 
{
	static CreditsState instance;
	return &instance;
}

CreditsState::~CreditsState()
{

}

void CreditsState::InitCredits()
{
	if(Game::GetInstance()->GetLanguage() == true)
	{
		credits[0].name = "SGP Studios\nPresents";
		credits[0].x = 100;
		credits[0].y = 100;
		credits[0].camX = 0;
		credits[0].camY = 0;

		credits[1].name = "A BoomSauce Games\nProduction";
		credits[1].x = 900;
		credits[1].y = 2100;
		credits[1].camX = 800;
		credits[1].camY = 1800;

		credits[2].name = "RoBHERT";
		credits[2].x = 2800;
		credits[2].y = 1000;
		credits[2].camX = 2400;
		credits[2].camY = 600;

		credits[3].name = "Executive Producer\nJohn O'Leske";
		credits[3].x = 100;
		credits[3].y = 1600;
		credits[3].camX = 0;
		credits[3].camY = 1200;

		credits[4].name = "Associate Producer\nRobert Martinez";
		credits[4].x = 2500;
		credits[4].y = 200;
		credits[4].camX = 2400;
		credits[4].camY = 0;

		credits[5].name = "Artist\nMaher Sagrillo";
		credits[5].x = 1700;
		credits[5].y = 1400;
		credits[5].camX = 1600;
		credits[5].camY = 1200;

		credits[6].name = "Programmer\nBrandon Jones";
		credits[6].x = 2600;
		credits[6].y = 1300;
		credits[6].camX = 2400;
		credits[6].camY = 1200;

		credits[7].name = "Programmer\nSean James";
		credits[7].x = 1900;
		credits[7].y = 400;
		credits[7].camX = 1600;
		credits[7].camY = 0;

		credits[8].name = "Programmer\nColby Yeates";
		credits[8].x = 900;
		credits[8].y = 1300;
		credits[8].camX = 800;
		credits[8].camY = 1200;

		credits[9].name = "Programmer\nBenjamin Feldmann";
		credits[9].x = 100;
		credits[9].y = 900;
		credits[9].camX = 0;
		credits[9].camY = 600;

		credits[10].name = "Menu Music\nG Empire Music &\n     Kelvin Galloza ";
		credits[10].x = 1700;
		credits[10].y = 700;
		credits[10].camX = 1600;
		credits[10].camY = 600;

		credits[11].name = "Game Music\nJack Wall &\n     Sam Hulick";
		credits[11].x = 100;
		credits[11].y = 2100;
		credits[11].camX = 0;
		credits[11].camY = 1800;

		credits[12].name = "Thanks For\nPlaying";
		credits[12].x = 1100;
		credits[12].y = 1000;
		credits[12].camX = 800;
		credits[12].camY = 600;
	}
	else
	{
		credits[0].name = "SGP Studios\n     Voorstellen";
		credits[0].x = 100;
		credits[0].y = 100;
		credits[0].camX = 0;
		credits[0].camY = 0;

		credits[1].name = "Een BoomSauce Games\n     Productie";
		credits[1].x = 900;
		credits[1].y = 2100;
		credits[1].camX = 800;
		credits[1].camY = 1800;

		credits[2].name = "RoBHERT";
		credits[2].x = 2800;
		credits[2].y = 1000;
		credits[2].camX = 2400;
		credits[2].camY = 600;

		credits[3].name = "Uitvoerend\n     Producent\n          John O'Leske";
		credits[3].x = 100;
		credits[3].y = 1400;
		credits[3].camX = 0;
		credits[3].camY = 1200;

		credits[4].name = "Associate Producent\n     Robert Martinez";
		credits[4].x = 2500;
		credits[4].y = 200;
		credits[4].camX = 2400;
		credits[4].camY = 0;

		credits[5].name = "Kunstenaar\n     Maher Sagrillo";
		credits[5].x = 1700;
		credits[5].y = 1400;
		credits[5].camX = 1600;
		credits[5].camY = 1200;

		credits[6].name = "Programmeur\n     Brandon Jones";
		credits[6].x = 2600;
		credits[6].y = 1300;
		credits[6].camX = 2400;
		credits[6].camY = 1200;

		credits[7].name = "Programmeur\n     Sean James";
		credits[7].x = 1900;
		credits[7].y = 400;
		credits[7].camX = 1600;
		credits[7].camY = 0;

		credits[8].name = "Programmeur\n     Colby Yeates";
		credits[8].x = 900;
		credits[8].y = 1300;
		credits[8].camX = 800;
		credits[8].camY = 1200;

		credits[9].name = "Programmeur\n     Benjamin\n          Feldmann";
		credits[9].x = 100;
		credits[9].y = 900;
		credits[9].camX = 0;
		credits[9].camY = 600;

		credits[10].name = "Menu Muziek\n     G Empire Music &\n          Kelvin Galloza ";
		credits[10].x = 1700;
		credits[10].y = 700;
		credits[10].camX = 1600;
		credits[10].camY = 600;

		credits[11].name = "Game Muziek\n     Jack Wall &\n          Sam Hulick";
		credits[11].x = 100;
		credits[11].y = 2100;
		credits[11].camX = 0;
		credits[11].camY = 1800;

		credits[12].name = "Bedankt Voor Het\n     Spelen Van";
		credits[12].x = 900;
		credits[12].y = 1000;
		credits[12].camX = 800;
		credits[12].camY = 600;
	}
}
